package com.ewanghuiju.app.widget.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.util.ClipboardUtils;
import com.ewanghuiju.app.util.ShareUtils;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.util.sharemoreimage.Tools;
import com.muddzdev.styleabletoast.StyleableToast;
import com.umeng.socialize.bean.SHARE_MEDIA;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import razerdp.basepopup.BasePopupWindow;

/**
 * @author sj  qq:751943509
 * @class 分享商品弹框
 * @time 2020/2/19
 */
public class ShareShopPopup extends BasePopupWindow {


    @BindView(R.id.share_shop_img)
    ImageView shareShopImg;

    private String imgUrl;
    private Activity mActivity;

    private String mMimeType;
    //    //团号
//    private String groupNO;
//    //商品标题
//    private String shopTitle;
//    //价格
//    private String price;
    //分享文本
    private String shareText;
    private ItemClickCallback mClickCallback;

    public ShareShopPopup(Context context, Activity activity, String shareText, ItemClickCallback clickCallback) {
        super(context);
        this.mActivity = activity;
        this.mClickCallback = clickCallback;
        this.shareText = shareText;
        ButterKnife.bind(this, getContentView());
    }

    @Override
    public View onCreateContentView() {
        return createPopupById(R.layout.popup_share_shop);
    }

    public void setImgUrl(String url) {
        this.imgUrl = url;
//        Glide.with(mActivity)
//                .load(imgUrl).placeholder(R.mipmap.error_picture)
//                .dontAnimate()//解决第一次加载不显示bug
//                .into(shareShopImg);
        Glide.with(mActivity).load(imgUrl).dontAnimate()
//                .placeholder(R.mipmap.icon_error_picture).error(R.mipmap.icon_error_picture)
                .placeholder(R.mipmap.icon_share_error_pic).error(R.mipmap.icon_share_error_pic)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(shareShopImg);
    }

    @OnClick({R.id.popup_close, R.id.tv_baocun, R.id.tv_wx, R.id.tv_friend, R.id.tv_qq, R.id.tv_copyurl})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.popup_close:
                dismiss();
                break;
            case R.id.tv_baocun:
                dismiss();
                if (mClickCallback != null) {
                    mClickCallback.saveImage(imgUrl);
                }
                break;
            case R.id.tv_wx:
                try {
                    dismiss();
                    if (!Tools.isAppAvilible(getContext(), "com.tencent.mm")) {
                        StyleableToast.makeText(mActivity, "您还没有安装微信", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    StyleableToast.makeText(mActivity, "请稍后...", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    ShareUtils.shareImageNet(mActivity, SHARE_MEDIA.WEIXIN, imgUrl, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_friend:
                try {
                    dismiss();
                    if (!Tools.isAppAvilible(getContext(), "com.tencent.mm")) {
                        StyleableToast.makeText(mActivity, "您还没有安装微信", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    StyleableToast.makeText(mActivity, "请稍后...", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    ShareUtils.shareImageNet(mActivity, SHARE_MEDIA.WEIXIN_CIRCLE, imgUrl, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_qq:
                try {
                    dismiss();
                    if (!Tools.isAppAvilible(getContext(), "com.tencent.mobileqq")) {
                        StyleableToast.makeText(mActivity, "您还没有安装QQ", Toast.LENGTH_SHORT, R.style.mytoast).show();
                        return;
                    }
                    StyleableToast.makeText(mActivity, "请稍后...", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    ShareUtils.shareImageNet(mActivity, SHARE_MEDIA.QQ, imgUrl, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_copyurl:
                dismiss();
                if (TextUtils.isEmpty(shareText)) {
                    StyleableToast.makeText(mActivity, "分享内容为空", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                if (!Tools.isAppAvilible(getContext(), "com.tencent.mm")) {
                    StyleableToast.makeText(mActivity, "您还没有安装微信", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                ClipboardUtils.copyText(shareText, "内容已复制到粘贴板");
//                StyleableToast.makeText(mActivity, "请稍后...", Toast.LENGTH_SHORT, R.style.mytoast).show();
                ShareUtils.shareText(mActivity, SHARE_MEDIA.WEIXIN, shareText, null);
                break;
            default:
                break;
        }
    }

    public interface ItemClickCallback {
        void saveImage(String url);
    }
}
